#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "interface.h"

/*
Reveal cards from your deck until you reveal 2 Treasure cards. 
Put those Treasure cards in your hand and discard the other revealed cards.
*/

int main(int argc, char const *argv[]){

	int i, r, p, randCard, copperCountBefore, copperCountAfter;
	// variables commented out due to warnings:
	// n, handCount, discardCount, deckCount,

  	int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};

  	int preCopper[2] = {0, 0}; // added by tim
  	int postCopper[2] = {0, 0}; // added by tim

	struct gameState G;
	
	int bonus = 0; // changed bonus to int instead of int*

	int j = 0; // added by tim
	int currentPlayer; // added by tim
	
	int choice1, choice2, choice3, handPos;
	choice1 = 0;
	choice2 = 0;
	choice3 = 0;
	handPos = 0;
	
  	SelectStream(2);
  	PutSeed(3);

	for(int m = 0; m < 10000; m++){
		if ( m % 70 == 0){
			r = initializeGame(2, k, 1, &G);
		}	    
		currentPlayer = whoseTurn(&G); // determine current player - added by tim
		p = floor(Random() * 2);
	    G.deckCount[p] = floor(Random() * MAX_DECK);
	    G.discardCount[p] = floor(Random() * MAX_DECK);
	    G.handCount[p] = floor(Random() * MAX_HAND);

		// generate a hand
		for (i = 0; i < G.handCount[p]; i++){
			randCard = floor(Random() * treasure_map);
			
			if (m > 100 && m < 200){
				if (randCard == 4){
					randCard = 0; // TC: fixed warning here
				}
			}
			
			// count the copper before
			if (randCard == copper){
				copperCountBefore++;
			}
			
			G.hand[p][i] = randCard;	
		}

		// Tim - Grab coppers in hand currently
		for(i = 0; i < G.numPlayers; i++){ // iterate through players
			if(i != currentPlayer){
				for(j = 0; j < G.handCount[i]; j++){ // iterate through hand
					if(G.hand[i][j] == copper){ //if hand contains copper
						preCopper[i] += 1; // add to count
					}
				}
			}
		}

		//G.whoseTurn = p;
		// TC: Added & to bonus
		r = cardEffect(cutpurse, choice1, choice2, choice3, &G, handPos, &bonus);
		
		// count copper after
		for (i = 0; i < G.handCount[p]; i++){
			if (G.hand[p][i] == copper){
				copperCountAfter++;
			}
		}

		// Tim - Grab coppers in hand currently
		for(i = 0; i < G.numPlayers; i++){ // iterate through players
			if(i != currentPlayer){
				for(j = 0; j < G.handCount[i]; j++){ // iterate through hand
					if(G.hand[i][j] == copper){ //if hand contains copper
						postCopper[i] += 1; // add to count
					}
				}
			}
		}
		
		char * fail = NULL; // TC: added NULL initializer here
		
		if(copperCountBefore > 0){
			if(copperCountBefore - copperCountAfter != 1){
				fail = "FAIL\0";
			}else{
				fail = "    \0";
			}
		}
		printf("Copper count before: %2d \t after:%2d \t%s\n", copperCountBefore, copperCountAfter, fail);

		// Begin Tim sanity check
		printf("BEGIN SANITY CHECK BY TIM\n");
		printf("Current active player is Player %d.\n", currentPlayer);
		for(i = 0; i < G.numPlayers; i++){ // iterate through players
			if(i != currentPlayer){
				if(preCopper[i] > 0){
					printf("Player %d Before: %d. After: %d\n", i, preCopper[i], postCopper[i]);
					assert(preCopper[i] == postCopper[i] + 1);
				}else{
					printf("Player %d did not have copper coins, SKIP.\n", i);
				}
			}
		}

		assert(r == 0);
		copperCountBefore = 0;
		copperCountAfter = 0;
		// added by tim
		preCopper[0] = 0;
		preCopper[1] = 0;
		postCopper[0] = 0;
		postCopper[1] = 0;
	}
	
	
	printf("All tests passed\n");

	return 0;
}
